/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import java.awt.Color;

/**
 *
 * @author Ripper
 */
public class ServerMessageProcessor 
{
    private ClientGameSession session;
    
    public ServerMessageProcessor(ClientGameSession cgs)
    {
        session = cgs;
    }
    
    public void processData(String data)
    {
        int colonIndex = data.indexOf(':');
        String dat = data.substring(colonIndex+1);
        if(data.startsWith("globallocations:"))
        {
            String[] characters = dat.split("-");
            
            for(String ent: characters)
            {
                processCharacterInfo(ent);
            }
        }
        else if(data.startsWith("m_add"))
        {
            addMonster(dat);
        }
        else if(data.startsWith("p_add"))
        {
            addPlayer(dat);
        }
        else if(data.startsWith("prj_add"))
        {
            addProjectile(dat);
        }
        else if(data.startsWith("prj_rm"))
        {
            try
            {
                int id = Integer.parseInt(dat);
                removeProjectile(id);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("de_add"))
        {
            addDecorativeElement(dat);
        }
        else if(data.startsWith("pln_add"))
        {
            addPlain(dat);
        }
        else if(data.startsWith("p_rm"))
        {
            try
            {
                String playername = dat;
                removePlayer(playername);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("hitpoints"))
        {
            String[] pieces = dat.split("/");
            int hp = Integer.parseInt(pieces[0]);
            int maxhp = Integer.parseInt(pieces[1]);
            
            session.setHitpoints(hp);
            session.setMaxhitpoints(maxhp);
        }
        else if(data.startsWith("mana"))
        {
            String[] pieces = dat.split("/");
            int mp = Integer.parseInt(pieces[0]);
            int maxmp = Integer.parseInt(pieces[1]);
            
            session.setManapoints(mp);
            session.setMaxmanapoints(maxmp);
        }
        else if(data.startsWith("stam"))
        {
            String[] pieces = dat.split("/");
            int sp = Integer.parseInt(pieces[0]);
            int maxsp = Integer.parseInt(pieces[1]);
            
            session.setStaminapoints(sp);
            session.setMaxstaminapoints(maxsp);
        }
        else if(data.startsWith("m_rm"))
        {
            try
            {
                int id = Integer.parseInt(dat);
                removeMonster(id);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("de_rm"))
        {
            try
            {
                int id = Integer.parseInt(dat);
                removeDecorativeElement(id);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("plrSpoken"))
        {
            try
            {
                String text = data.split(":")[1];
                String plrName = text.split("=")[0];
                String encodedText = text.split("=")[1];
                
                session.playerHasSpoken(plrName, encodedText);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("xloc"))
        {
            try
            {
                String loc = data.split(":")[1];
                
                Double x = Double.parseDouble(loc);
                
                session.setActivePlayersX(x);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("yloc"))
        {
            try
            {
                String loc = data.split(":")[1];
                
                Double y = Double.parseDouble(loc);
                
                session.setActivePlayersY(y);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith("showtxteffect"))
        {
            try
            {
                String datapart = data.split(":")[1];
                String[] databits = datapart.split(",");
                
                int r = Integer.parseInt(databits[0]);
                int g = Integer.parseInt(databits[1]);
                int b = Integer.parseInt(databits[2]);
                int x = (int)(Double.parseDouble(databits[3]));
                int y = (int)(Double.parseDouble(databits[4]));
                String text = databits[5];
                
                Color c = new Color(r,g,b);
                session.addTextEffect(c,x,y,text);
            }
            catch(Exception ex){}
        }
        else if(data.startsWith(""))
        {
            
        }
    }
    public void addPlain(String data)
    {
        try
        {
            String[] bits = data.split(",");
            
            double x = Double.parseDouble(bits[0].split(":")[1]);
            double y = Double.parseDouble(bits[1].split(":")[1]);
            int nSize = Integer.parseInt(bits[2].split(":")[1]);
            int mSize = Integer.parseInt(bits[3].split(":")[1]);
            int tileSize = Integer.parseInt(bits[4].split(":")[1]);
            
            String content = bits[5];
            int startIdx = -1;
            int endIdx = -1;
            
            int row = 0;
            while(content.length() > 3)
            {
                startIdx = content.indexOf('(');
                endIdx = content.indexOf(')');
                
                String rowData = content.substring(startIdx+1, endIdx);
                
                String tileBits[] = rowData.split("-");
                int column = 0;
                for(String bit: tileBits)
                {
                    ClientTile ct = new ClientTile(session, Integer.parseInt(bit), 
                            x + (tileSize * column), y + (tileSize * row), tileSize);
                    session.addTile(ct);
                    column++;
                }
                
                row++;
                content = content.substring(endIdx+1);
            }
            //[(id-id-id-id)(id-id-id-id)]
            
        }
        catch(Exception ex)
        {
        }
    }
    public void addDecorativeElement(String data)
    {
        try
        {
            String[] bits = data.split(",");
            
            int id = Integer.parseInt(bits[0]);
            int vis_id = Integer.parseInt(bits[1]);
            double x = Double.parseDouble(bits[2]);
            double y = Double.parseDouble(bits[3]);
            
            Color[] colors = new Color[4];
            for(int a = 0; a < 4; a++)
            {
                String colorBit = bits[a+4];
                String[] omfgwtfshouldikeepnamingthesethings = colorBit.split("=");
                
                int r = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[0]);
                int g = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[1]);
                int b = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[2]);
                colors[a] = new Color(r,g,b);
            }
            
            ClientDecorativeElement cde = new ClientDecorativeElement(session, id, vis_id, x, y, colors);
            session.addDecorativeElement(cde);
        }
        catch(Exception ex){}
    }
    public void addProjectile(String data)
    {
        try
        {
            String[] bits = data.split(",");
            
            int id = Integer.parseInt(bits[0].split(":")[1]);
            double x = Double.parseDouble(bits[1].split(":")[1]);
            double y = Double.parseDouble(bits[2].split(":")[1]);
            //movespeed is 3
            int looktype = Integer.parseInt(bits[4].split(":")[1]);
            double rotation = Double.parseDouble(bits[5].split(":")[1]);
            
            ClientProjectile cp = new ClientProjectile(session, id, looktype, x, y, rotation);
            session.addProjectile(cp);
        }
        catch(Exception ex){}
    }
    public void addPlayer(String data)
    {
        try
        {
            String[] bits = data.split(",");
            String[] locationBit = new String[7];
            
            String plrnm = bits[bits.length-1].split(":")[1];
            for(int a = 0; a < 6; a++)
            {
                locationBit[a+1] = bits[a];
            }
            locationBit[0] = plrnm;
            
            int hpprct = Integer.parseInt(bits[7].split(":")[1]);
            
            String looktypestring = bits[8].split(":")[1];
            int looktype = Integer.parseInt(looktypestring);
            Color[] colors = new Color[4];
            for(int a = 9; a < 13; a++)
            {
                String colorBits = bits[a].split(":")[1];
                String[] omfgwtfshouldikeepnamingthesethings = colorBits.split("=");
                
                int r = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[0]);
                int g = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[1]);
                int b = Integer.parseInt(omfgwtfshouldikeepnamingthesethings[2]);
                colors[a-9] = new Color(r,g,b);
            }
            
            ClientPlayer cp = new ClientPlayer(session, 0, 0, 100, looktype, colors, plrnm);
            cp.setHpprct(hpprct);
            session.addPlayer(cp);
            
            String charInfo = "";
            for(String s: locationBit)
            {
                charInfo += s + ",";
            }
            charInfo = charInfo.substring(0, charInfo.length()-1);
            processCharacterInfo(charInfo);
        }
        catch(Exception ex){}
    }
    public void addMonster(String data)
    {
        try
        {
            String[] bits = data.split(",");
            String[] locationBit = new String[7];
            
            String mobname = bits[bits.length - 2].split(":")[1];
            int mobID = Integer.parseInt(bits[bits.length-1].split(":")[1]);
            for(int a = 0; a < 6; a++)
            {
                locationBit[a+1] = bits[a];
            }
            locationBit[0] = bits[bits.length-1];
            
            String looktypestring = bits[8];
            int looktype = Integer.parseInt(looktypestring.split(":")[1]);
            Color[] colors = new Color[4];
            for(int a = 9; a < 13; a++)
            {
                String colorBits = bits[a].split(":")[1];
                String[] omfgwtfshouldikeepnamingthesethings = colorBits.split("=");
                
                colors[a-9] = new Color(Integer.parseInt(omfgwtfshouldikeepnamingthesethings[0]),
                                        Integer.parseInt(omfgwtfshouldikeepnamingthesethings[1]),
                                        Integer.parseInt(omfgwtfshouldikeepnamingthesethings[2]));
            }
            
            ClientMob cm = new ClientMob(session, 0, 0, 100, looktype, colors, mobname, mobID);
            session.addMonster(cm);
            
            String charInfo = "";
            for(String s: locationBit)
            {
                charInfo += s + ",";
            }
            charInfo = charInfo.substring(0, charInfo.length()-1);
            processCharacterInfo(charInfo);
        }
        catch(Exception ex){}
    }
    public void removePlayer(String plr)
    {
        session.removePlayer(plr);
    }
    public void removeMonster(int id)
    {
        session.removeMonster(id);
    }
    public void removeDecorativeElement(int id)
    {
        session.removeDecorativeElement(id);
    }
    public void removeProjectile(int id)
    {
        session.removeProjectile(id);
    }
    
    public void processCharacterInfo(String charInfo)
    {
        //System.out.println("processing: "+charInfo);
        if(charInfo.startsWith("prj"))
        {
            charInfo = charInfo.substring(3);
            try
            {
                String[] bitsOfInfo = charInfo.split(",");
                
                int id = Integer.parseInt(bitsOfInfo[0].split(":")[1]);
                
                double x = Double.parseDouble(bitsOfInfo[1].split(":")[1]);
                double y = Double.parseDouble(bitsOfInfo[2].split(":")[1]);
                
                for(ClientProjectile cp: session.getProjectiles())
                {
                    if(cp.getId() == id)
                    {
                        cp.setX(x);
                        cp.setY(y);
                    }
                }
            }
            catch(Exception ex)
            {
                
            }
        }
        else
        {
            try
            {
                String[] bitsOfInfo = charInfo.split(",");

                ClientEntity relevantCharacter = null;
                String id = bitsOfInfo[0];
                if(id.startsWith("p_nm:"))
                {
                    String plrName = id.split(":")[1];

                    relevantCharacter = session.getPlayer(plrName);
                }
                else if(id.startsWith("m_id:"))
                {
                    String mobID = id.split(":")[1];
                    int m_id = Integer.parseInt(mobID);

                    relevantCharacter = session.getMonster(m_id);
                }

                if(relevantCharacter != null)
                {
                    for(int a = 1; a < bitsOfInfo.length;a++)
                    {
                        String bit = bitsOfInfo[a];

                        String idpart = bit.split(":")[0];
                        String datapart = bit.split(":")[1];

                        if(idpart.equals("x"))
                        {
                            double x = Double.parseDouble(datapart);
                            relevantCharacter.setX(x);
                        }
                        else if(idpart.equals("y"))
                        {
                            double y = Double.parseDouble(datapart);
                            relevantCharacter.setY(y);
                        }
                        else if(idpart.equals("mn"))
                        {
                            boolean mn = Boolean.parseBoolean(datapart);
                            relevantCharacter.setMovingNorth(mn);
                        }
                        else if(idpart.equals("ms"))
                        {
                            boolean ms = Boolean.parseBoolean(datapart);
                            relevantCharacter.setMovingSouth(ms);
                        }
                        else if(idpart.equals("me"))
                        {
                            boolean me = Boolean.parseBoolean(datapart);
                            relevantCharacter.setMovingEast(me);
                        }
                        else if(idpart.equals("mw"))
                        {
                            boolean mw = Boolean.parseBoolean(datapart);
                            relevantCharacter.setMovingWest(mw);
                        }
                        else if(idpart.equals("hp"))
                        {
                            int hp = Integer.parseInt(datapart);
                            relevantCharacter.setHpprct(hp);
                        }
                    }
                }
            }
            catch(Exception ex) {}
        }
    }
}
